#-------------------------------------------------------------------------------
# DEPENDENCY GENERATION

# NB: I am not 100% happy with the names of _bin_filename et al, but it does
#     make the dependency code clearer, by factoring out common patterns.

# The target.maks will get rebuilt if anything in the build system is edited.
# It's slower, but more humane this way...
MAKE_DEPENDENCIES:=makefile $(wildcard make/*) $(wildcard make/rules/*)

#use -V flag for source_dep.awk if VERBOSE=1
ifeq ($(call check-flag,$(VERBOSE)),1)
_source_dep_v:=-V
else
_source_dep_v:=
endif

define make_target_dep
	@$(MKDIR) $(dir $@)
	@$(ECHO) [MAK] Calculating dependencies for \'$<\'
	@make/target_dep.sh $(dir $<) $(notdir $<) $(dir $@) $(notdir $@) > $@
endef

# target.mak dependencies
dep/$(TYPE)/%/target.mak: src/%/target.mak $(MAKE_DEPENDENCIES)
	$(call make_target_dep)

# rule for src/target.mak specifically
dep/$(TYPE)/target.mak: src/target.mak $(MAKE_DEPENDENCIES)
	$(call make_target_dep)

# rule dupe for 'sr3'
dep/$(TYPE)/%/target.mak: sr3/%/target.mak $(MAKE_DEPENDENCIES)
	$(call make_target_dep)


# binary file dependencies

_bin_filename=$(@:dep/%.d=bin/%)
define make_bin_target
	@$(MKDIR) $(dir $@)
	@$(ECHO) [BIN] Calculating dependencies for \'$(_bin_filename)\'
	@$(ECHO) "# Autogenerated dependencies for $(_bin_filename)" > $@
	@$(ECHO) $($(_bin_filename)_DEPFILE): $($(_bin_filename)_TARGETFILE) >> $@
	@$(ECHO) $@: $($(_bin_filename)_DEPFILE) >> $@
	@$(ECHO) $(_bin_filename): $@ $($(_bin_filename)_LIB) $($(_bin_filename)_OBJ) >> $@
endef

dep/%$(BINEXT).d:
	$(call make_bin_target)


# dll dependencies
# TODO: needs rehabilitation

_dll_filename=$(@:dep/%.d=bin/%)
define make_dll_target
	@$(MKDIR) $(dir $@)
	@$(ECHO) [DLL] Calculating dependencies for \'$(_dll_filename)\'
	@$(ECHO) "# Autogenerated dependencies for $(_dll_filename)" >> $@
	@$(ECHO) $($(_dll_filename)_DEPFILE): $($(_dll_filename)_TARGETFILE) > $@
	@$(ECHO) $@: $($(_dll_filename)_DEPFILE) >> $@
	@$(ECHO) $(_dll_filename): $@ $($(_dll_filename)_LIB) $($(_dll_filename)_OBJ) >> $@
endef

dep/%$(DLLEXT).d:
	$(call make_dll_target)


# lib dependencies

_lib_filename=$(@:dep/%.d=lib/%)
define make_lib_target
	@$(MKDIR) $(dir $@)
	@$(ECHO) [LIB] Calculating dependencies for \'$(_lib_filename)\'
	@$(ECHO) "# Autogenerated dependencies for $(_lib_filename)" > $@
	@$(ECHO) $($(_lib_filename)_DEPFILE): $($(_lib_filename)_TARGETFILE) >> $@
	@$(ECHO) $@: $($(_lib_filename)_DEPFILE) >> $@
	@$(ECHO) $(_lib_filename): $@ $($(_lib_filename)_OBJ) >> $@
endef

dep/%$(LIBEXT).d:
	$(call make_lib_target)


# c/c++ (source) file dependencies

source_dep_includes=$(foreach include, $(sort $(INCLUDES)), -I $(include))
define make_source_dep
	@$(MKDIR) $(dir $@)
	make/source_dep.awk $< $@ $(<:$(1)/%=obj/$(TYPE)/%.o) -I src -I sr3 $(source_dep_includes) -G obj $(_source_dep_v)
endef

dep/$(TYPE)/%.c.d: src/%.c
	$(call make_source_dep,src)

dep/$(TYPE)/%.cpp.d: src/%.cpp
	$(call make_source_dep,src)

# rule dupes for 'sr3'
dep/$(TYPE)/%.c.d: sr3/%.c
	$(call make_source_dep,sr3)

dep/$(TYPE)/%.cpp.d: sr3/%.cpp
	$(call make_source_dep,sr3)


